package com.open.capacity.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.open.capacity.common.constant.PointsRuleConstant;
import com.open.capacity.common.web.Result;
import com.open.capacity.forum.dao.ResourcesBuyRecordDao;
import com.open.capacity.forum.dao.ResourcesDownRecordDao;
import com.open.capacity.forum.feign.PointsCenterService;
import com.open.capacity.forum.feign.UserCenterService;
import com.open.capacity.forum.model.ResourcesBuyRecord;
import com.open.capacity.forum.model.ResourcesDownRecord;
import com.open.capacity.forum.model.ResourcesFile;
import com.open.capacity.forum.service.ResourcesBuyRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 蒋鑫
 * @version 2021/3/8
 */
@Service
public class ResourceBuyRecordServiceImpl implements ResourcesBuyRecordService {
    @Resource
    ResourcesBuyRecordDao resourcesBuyRecordDao;

    @Resource
    private PointsCenterService pointsCenterService;

    @Resource
    private ResourcesDownRecordDao resourcesDownRecordDao;

    @Resource
    UserCenterService userCenterService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result buyResource(Map<String, Object> map) {
        //判断资源是否需要积分
        int point = (int) map.get("points");
        long id = Long.valueOf((String) map.get("userid"));
        if (point > 0) {
            //判断用户是否购买过
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("userid", map.get("userid"));
            hashMap.put("fileid", map.get("fileid"));
            List<ResourcesBuyRecord> resourcesBuyRecord = resourcesBuyRecordDao.selectByMap(hashMap);
            if (resourcesBuyRecord.size() != 0) {
                //购买过
                return Result.succeed("购买成功");
            } else {
                //判断用户积分是否足够
                //通过用户id去查询用户积分
                int userHavePoints = pointsCenterService.findPointsByUserId(id).getData();
                if (point < userHavePoints) {
                    //积分足够,可以进行购买
                    pointsCenterService.change(id, PointsRuleConstant.DOWNLOAD_RES, point);
                    //购买后生成一条购买记录
                    ResourcesBuyRecord rbr = new ResourcesBuyRecord();
                    rbr.setCretim(new Date());
                    rbr.setFileid((int) map.get("fileid"));
                    rbr.setUserid(Long.valueOf((String) map.get("userid")));
                    int result = resourcesBuyRecordDao.insert(rbr);
                    if (result > 0) {
                        return Result.succeed("购买成功");
                    } else {
                        return Result.failed("购买失败");
                    }
                } else {
                    return Result.failed("积分不足");
                }
            }
        } else {
            //不需要积分直接下载
            return Result.succeed("购买成功");
        }
    }

    @Override
    public Result buyRecords(Map<String, Object> map) {
        //判断有没有下载该资源的记录
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("userid", map.get("userid"));
        hashMap.put("fileid", map.get("fileid"));
        ResourcesDownRecord rdr = new ResourcesDownRecord();
        rdr.setCretim(new Date());
        rdr.setFileid((int) hashMap.get("fileid"));
        rdr.setUserid(Long.valueOf((String) hashMap.get("userid")));
        if (resourcesDownRecordDao.selectByMap(hashMap).size() != 0) {
            return Result.succeed("该记录已经存在");
        } else {
            //没有记录就生成一条下载记录
            int result = resourcesDownRecordDao.insert(rdr);
            if (result > 0) {
                //更新下载的称号
                QueryWrapper<ResourcesDownRecord> wrapper = new QueryWrapper();
                wrapper.eq("userid", rdr.getUserid());
                Integer count = resourcesDownRecordDao.selectCount(wrapper);
                Result upload = userCenterService.updateTitleByUserIdAndPoints(rdr.getUserid(), count.doubleValue(), "DOWNLOAD");
            }
            return Result.succeed("记录添加成功");
        }
    }
}
